package com.cinc.ebc.mm.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cinc.ebc.mm.entity.MmMaterialPickingRet;
import com.cinc.ebc.mm.entity.MmMaterialPickingRetDetail;
import com.cinc.ebc.mm.entity.MmStockInOut;
import com.cinc.ebc.mm.entity.MmStockInOutDetail;
import com.cinc.ebc.mm.enums.MaterialPickingRetStatusEnum;
import com.cinc.ebc.mm.mapper.MmMaterialPickingRetDetailMapper;
import com.cinc.ebc.mm.mapper.MmMaterialPickingRetMapper;
import com.cinc.ebc.mm.service.IMmMaterialPickingRetService;
import com.cinc.ebc.mm.service.IMmStockInOutService;
import com.cinc.ebc.mm.vo.MmMaterialPickingRetRequestPage;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * @Description: 材料退回
 * @Author: jeecg-boot
 * @Date:   2023-04-16
 * @Version: V1.0
 */
@Service
public class MmMaterialPickingRetServiceImpl extends ServiceImpl<MmMaterialPickingRetMapper, MmMaterialPickingRet> implements IMmMaterialPickingRetService {

	@Autowired
	private MmMaterialPickingRetMapper mmMaterialPickingRetMapper;
	@Autowired
	private MmMaterialPickingRetDetailMapper mmMaterialPickingRetDetailMapper;

	@Autowired
	private IMmStockInOutService mmStockInOutService;


	@Override
	@Transactional
	public void saveMain(MmMaterialPickingRet mmMaterialPickingRet, List<MmMaterialPickingRetDetail> mmMaterialPickingRetDetailList) {
		mmMaterialPickingRetMapper.insert(mmMaterialPickingRet);
		if(mmMaterialPickingRetDetailList!=null && mmMaterialPickingRetDetailList.size()>0) {
			for(MmMaterialPickingRetDetail entity:mmMaterialPickingRetDetailList) {
				//外键设置
				entity.setMainId(mmMaterialPickingRet.getId());
				mmMaterialPickingRetDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(MmMaterialPickingRet mmMaterialPickingRet,List<MmMaterialPickingRetDetail> mmMaterialPickingRetDetailList) {
		MmMaterialPickingRet mmMaterialPickingRetOld = mmMaterialPickingRetMapper.selectById(mmMaterialPickingRet.getId());
		if(!mmMaterialPickingRetOld.getStatus().equals(MaterialPickingRetStatusEnum.NEW.getCode())){
			throw new RuntimeException("退料单已经开始处理，不能修改");
		}
		mmMaterialPickingRetMapper.updateById(mmMaterialPickingRet);

		//1.先删除子表数据
		mmMaterialPickingRetDetailMapper.deleteByMainId(mmMaterialPickingRet.getId());

		//2.子表数据重新插入
		if(mmMaterialPickingRetDetailList!=null && mmMaterialPickingRetDetailList.size()>0) {
			for(MmMaterialPickingRetDetail entity:mmMaterialPickingRetDetailList) {
				//外键设置
				entity.setMainId(mmMaterialPickingRet.getId());
				mmMaterialPickingRetDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		MmMaterialPickingRet mmMaterialPickingRet = mmMaterialPickingRetMapper.selectById(id);
		if(!mmMaterialPickingRet.getStatus().equals(MaterialPickingRetStatusEnum.NEW.getCode())){
			throw new RuntimeException("退料单已经开始处理，不能删除");
		}
		mmMaterialPickingRetDetailMapper.deleteByMainId(id);
		mmMaterialPickingRetMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			MmMaterialPickingRet mmMaterialPickingRet = mmMaterialPickingRetMapper.selectById(id);
			if(!mmMaterialPickingRet.getStatus().equals(MaterialPickingRetStatusEnum.NEW.getCode())){
				throw new RuntimeException("退料单已经开始处理，不能删除");
			}
			mmMaterialPickingRetDetailMapper.deleteByMainId(id.toString());
			mmMaterialPickingRetMapper.deleteById(id);
		}
	}

	@Transactional
	@Override
	public void handleRet(String id) throws Exception {
		MmMaterialPickingRet materialPickingRet = mmMaterialPickingRetMapper.selectById(id);
		if(oConvertUtils.isEmpty(materialPickingRet.getWarehouseId())){
			throw new RuntimeException("库房不能为空");
		}
		List<MmMaterialPickingRetDetail> materialPickingRetDetailList = mmMaterialPickingRetDetailMapper.selectByMainId(id);
		if(oConvertUtils.listIsEmpty(materialPickingRetDetailList)){
			throw new RuntimeException("请维护退料明细!");
		}
		// 更新退料明细
		mmMaterialPickingRetDetailMapper.updateOutQtyFinishByMinId(id);
		// 更新领料主表
		mmMaterialPickingRetMapper.autoUpdateStatusById(id);
		//
		int stockInOutLineNo = 0;
		List<MmStockInOutDetail> stockInOutDetailList = new ArrayList<>();
		for(MmMaterialPickingRetDetail materialPickingRetDetail : materialPickingRetDetailList){
			// 出库明细
			MmStockInOutDetail stockInOutDetail = new MmStockInOutDetail();
			stockInOutLineNo += 10;
			stockInOutDetail.setLineNo(stockInOutLineNo);
			stockInOutDetail.setMaterialId(materialPickingRetDetail.getMaterialId());
			stockInOutDetail.setMaterialNo(materialPickingRetDetail.getMaterialNo());
			stockInOutDetail.setMaterialDesc(materialPickingRetDetail.getMaterialDesc());
			stockInOutDetail.setUnit(materialPickingRetDetail.getUnit());
			stockInOutDetail.setQty(materialPickingRetDetail.getQty().negate());
			stockInOutDetail.setUnitPrice(materialPickingRetDetail.getUnitPrice());
			stockInOutDetail.setSumPrice(materialPickingRetDetail.getSumPrice().negate());
			stockInOutDetail.setSourceId(materialPickingRetDetail.getId());
			stockInOutDetail.setSourceNo(materialPickingRet.getBillNo());
			stockInOutDetail.setWarehouseId(materialPickingRet.getWarehouseId());
			stockInOutDetail.setStorageLocationId(materialPickingRetDetail.getStorageLocationId());
			stockInOutDetail.setBatchNo(materialPickingRetDetail.getBatchNo());
			stockInOutDetail.setProjectId(materialPickingRet.getProjectId());

			stockInOutDetailList.add(stockInOutDetail);
		}
		// 生成出库单
		MmStockInOut stockInOut = new MmStockInOut();
		stockInOut.setBillDate(new Date());
		stockInOut.setProjectId(materialPickingRet.getProjectId());
		mmStockInOutService.saveMainMaterialPickingRet(stockInOut, stockInOutDetailList, materialPickingRet);



	}

	@Override
	public IPage<MmMaterialPickingRet> pickingRetPage(Page<MmMaterialPickingRet> page, MmMaterialPickingRetRequestPage mmMaterialPickingRetRequest) {
		String sql = QueryGenerator.installAuthJdbc(MmMaterialPickingRet.class);
		return page.setRecords(baseMapper.selectPageList(page, mmMaterialPickingRetRequest, sql));
	}

}
